<?php
include_once('inc/functions.inc.php');

if(!isEmpty(getParam('operation'))){
    
    switch(getParam('operation')){
        case 'contenido': {
            fntAcademicoPensumContenido($db);
            break;
        }
        case 'reporte': {
            fntAcademicoPensumReporte($db);
            break;
        }
        case 's': {
            fntAcademicoPensumGrabar($db);
            break;
        }
    }
    die();
}

function fntAcademicoPensumContenido($db){
    //VARIBLES
    $strId = "";
    $strQuery = "";
    $arrInfo = array();
    $strCodigo = "";
    $strNombre = "";
    $strSelected = "";
    //VARIBLES
    
    $strId = isset($_POST["hdnAcademicoPensum_Id"]) ? $_POST["hdnAcademicoPensum_Id"] : "";
    if( !empty($strId) ) {
        $strQuery = "SELECT CODIGO, FK_PERIODO, FK_GRADO, 
                            DATE_FORMAT(VIGENCIA_INICIAL, '%d-%m-%Y') AS VIGENCIA_INICIAL, 
                            DATE_FORMAT(VIGENCIA_FINAL, '%d-%m-%Y') AS VIGENCIA_FINAL 
                    FROM cllg_academico_pensum WHERE ID = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ( $rTMP = $db->fetch_array($qTMP) ) { 
            $arrInfo = $rTMP;        
        }
        $db->free_result($qTMP);
    }
    
    $strCodigo = isset($arrInfo["CODIGO"]) ? $arrInfo["CODIGO"] : "";
    $strPeriodo = isset($arrInfo["FK_PERIODO"]) ? $arrInfo["FK_PERIODO"] : "";
    $strGrado = isset($arrInfo["FK_GRADO"]) ? $arrInfo["FK_GRADO"] : "";
    $strVigenciaInicial = isset($arrInfo["VIGENCIA_INICIAL"]) ? $arrInfo["VIGENCIA_INICIAL"] : "";
    $strVigenciaFinal = isset($arrInfo["VIGENCIA_FINAL"]) ? $arrInfo["VIGENCIA_FINAL"] : "";
    
    
    ?>
    <br>
    <br>
    <div style="width: 150px; height: 30px; float: left;"><b>Codigo</b></div>
    <div style="width: 350px; height: 30px; float: left;"><input type="text" name="txtAcademicoPensum_codigo" value="<?php print $strCodigo; ?>"></div>
    <div style="width: 150px; height: 30px; float: left;"><b>Periodo</b></div>
    <div style="width: 350px; height: 30px; float: left;">
        <select name="txtAcademicoPensum_fk-periodo">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_periodos WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                $strSelected = ( $strPeriodo==$rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 150px; height: 30px; float: left;"><b>Grado</b></div>
    <div style="width: 350px; height: 30px; float: left;">
        <select name="txtAcademicoPensum_fk-grado">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_grados WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                $strSelected = ( $strGrado==$rTMP["ID"] ) ? "selected" : "";
                ?>
                <option value="<?php print $rTMP["ID"]; ?>" <?php print $strSelected; ?>><?php print $rTMP["CODIGO"]; ?> - <?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>
        </select>
    </div>
    <div style="width: 150px; height: 30px; float: left;"><b>Vigencia</b></div>
    <div style="width: 175px; height: 30px; float: left;">
        <b>Del:</b>
        <input type="text" name="txtAcademicoPensum_VIGENCIA-INICIAL" value="<?php print $strVigenciaInicial; ?>" maxlength="10" size="12" style="text-align: center;" readonly="readonly">
        </div>
    <div style="width: 175px; height: 30px; float: left;">
        <b>Al:</b>
        <input type="text" name="txtAcademicoPensum_VIGENCIA-FINAL" value="<?php print $strVigenciaFinal; ?>" maxlength="10" size="12" style="text-align: center;" readonly="readonly">
        </div>
    </div>
    <div style="width: 500px; height: 30px; float: left;">&nbsp;</div>
    <div style="width: 400px; height: 30px; float: left;">
        <b>Asignaturas</b>&nbsp;
        <select name="sltAcademicoPensumMaterias" id="sltAcademicoPensumMaterias">
            <option value=""></option>
            <?php
            $strQuery = "SELECT * FROM cllg_academico_materias WHERE ACTIVO = 'Y'";
            $qTMP = $db->query($strQuery);
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <option value="<?php print $rTMP["ID"]; ?>"><?php print $rTMP["CODIGO"]; ?> - <?php print $rTMP["NOMBRE"]; ?></option>
                <?php        
            }
            $db->free_result($qTMP);
            ?>        
        </select>
    </div>
    <div style="width: 100px; height: 30px; float: left;">
        <button type="button" id="btnAcademicoPensumAgregar">Agregar</button>
    </div>
    <div id="divAcademicoPensumMaterias" style="width: 500px; height: 100px; float: left; border: 1px solid black; overflow:auto;">
        <?php
        $strQuery = "SELECT cllg_academico_pensum_detail.*, cllg_academico_materias.ID AS MID, cllg_academico_materias.CODIGO AS MCODIGO, cllg_academico_materias.NOMBRE AS MNOMBRE    
                    FROM    cllg_academico_pensum_detail, cllg_academico_materias 
                    WHERE   cllg_academico_materias.ID = cllg_academico_pensum_detail.FK_MATERIA
                    AND     cllg_academico_pensum_detail.FK_PENSUM = '{$strId}'";
        $qTMP = $db->query($strQuery);
        while ( $rTMP = $db->fetch_array($qTMP) ) { 
            ?>
            <div style="width: 482px; height: 25px; float: left; line-height: 25px; vertical-align: middle; border-bottom: 1px solid black;">&nbsp;&nbsp;&nbsp;&nbsp;<?php print $rTMP["MCODIGO"]; ?> - <?php print $rTMP["MNOMBRE"]; ?></div>
            <input type="hidden" name="hdnAcademicoPensumD_<?php print $rTMP["MID"]; ?>" value="<?php print $rTMP["MID"]; ?>">
            <?php        
        }
        $db->free_result($qTMP);
        ?>    
    </div>
    <script type="text/javascript">
    $(function(){
        $("input[name='txtAcademicoPensum_VIGENCIA-INICIAL']").datepicker({
            changeMonth: true,
            changeYear: true,
            monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
            dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
            dateFormat: 'dd-mm-yy'
        });
        $("input[name='txtAcademicoPensum_VIGENCIA-FINAL']").datepicker({
            changeMonth: true,
            changeYear: true,
            monthNamesShort: ['Ene','Feb','Mar','Abr','May','Jun','Jul','Ago','Sep','Oct','Nov','Dic'],
            dayNamesMin: ['Do', 'Lu', 'Ma', 'Mi', 'Ju', 'Vi', 'Sa'],
            dateFormat: 'dd-mm-yy'
        });
        $("#btnAcademicoPensumAgregar").click( function () {
            fntAcademicoPensumMateriaAgregar();
        });    
    });
    </script>
    <?php
    
}
function fntAcademicoPensumReporte($db){

    //VARIBLES
    $strId = "";
    $strQuery = "";
    //VARIBLES
    
    $strQuery = "SELECT * FROM cllg_academico_pensum ORDER BY CODIGO";
    $qTMP = $db->query($strQuery);
    $intNumRows = $db->num_rows;
    
    ?>
    <style type="">
    table {
        border-collapse: collapse;
    }
    table td{
        border: 1px solid #AAAAAA;        
    }
    .oddtr {
        background-color:#FFFFFF;
    }
    .eventr {
        background-color:#FFFFFF;
    }
    .trover {
        background-color: #ECEAE0;
    }
    .trclick {
        background-color: #ECEAE0;
    }
    </style>
    <br>
    <br>
    <table width="100%" cellpadding="2" cellspacing="0" border="0">
        <tbody>
            <td width="50%" align="center" style="background-color: #ECEAE0;"><b>Codigo</b></td>
            <td width="25%" style="background-color: #ECEAE0;"><b>Vigencia Inicial</b></td>
            <td width="25%" style="background-color: #ECEAE0;"><b>Vigencia Final</b></td>
        </tbody>
    </table>
    <table width="100%" cellpadding="2" cellspacing="0" border="0" id="tblAcademicoPensum">   
        <?php    
        if( $intNumRows ) {
        
            while ( $rTMP = $db->fetch_array($qTMP) ) { 
                ?>
                <tr id="<?php print $rTMP["ID"]; ?>">
                    <td align="center" width="50%"><?php print $rTMP["CODIGO"]; ?></td>
                    <td width="25%" align="center"><?php print $rTMP["VIGENCIA_INICIAL"]; ?></td>
                    <td width="25%" align="center"><?php print $rTMP["VIGENCIA_FINAL"]; ?></td>
                </tr>        
                <?php
            }
            
        }
        $db->free_result($qTMP);
        ?>
    </table>
    <script type="">
    //these two line adds the color to each different row
    $("#tblAcademicoPensum tr:even").addClass("eventr");;
    $("#tblAcademicoPensum tr:odd").addClass("oddtr");;
    //handle the mouseover , mouseout and click event
    $("#tblAcademicoPensum tr").mouseover(function() {
        $(this).addClass("trover");
    }).mouseout(function() {
        $(this).removeClass("trover");
    }).click(function() {
        //$("tr[id!='"+$(this).attr("id")+"']").removeClass("trover");
        //$(this).addClass("trover");        
        //$(this).toggleClass("trclick");
        $("input[name='hdnAcademicoPensum_Id']").val( $(this).attr("id") );
        fntAcademicoPensumContenido();
    });
    </script>
    <?php
    
}
function fntAcademicoPensumGrabar($db){

    $strId = isset($_POST["hdnAcademicoPensum_Id"]) ? $_POST["hdnAcademicoPensum_Id"] : "";
    $strFields = "";
    $strValues = "";
    
    while( $arrPost = each($_POST) )  {
        $arrExplode = explode( "_", $arrPost["key"]);
        
        if( $arrExplode[0] == "txtAcademicoPensum" ) {
            if( $arrPost["key"] != "hdnAcademicoPensum_Id" ){
                
                if( $arrExplode[1] == "VIGENCIA-INICIAL" || $arrExplode[1] == "VIGENCIA-FINAL" ) {
                    $arrTMP = array();
                    $arrTMP = explode( "-", $arrPost["value"] );
                    krsort($arrTMP);
                    $arrPost["value"] = implode("-", $arrTMP);
                }
                
                if( empty($strId) ) {
                    $strFields .= (empty($strFields)) ? "" : ","; 
                    $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) );
                    
                    $strValues .= empty( $strValues ) ? "" : ",";
                    $strValues .= "'{$arrPost["value"]}'";
                }
                else {
                    $strFields .= (empty($strFields)) ? "" : ",";
                    $strFields .=  strtoupper( str_ireplace( "-", "_", $arrExplode[1] ) ). " = '{$arrPost["value"]}'"; 
                    
                }
            }
        }
        
    }
    if( empty($strId) ) {
        $db->query("INSERT INTO cllg_academico_pensum ({$strFields}) VALUES ({$strValues})");
        $intID = mysql_insert_id ( $db->link_id ); 
        reset($_POST);
        while( $arrPost1 = each($_POST) ) {
            $arrExplode = explode( "_", $arrPost1["key"]);
            if( $arrExplode[0] == "hdnAcademicoPensumD" ) {
                $db->query("INSERT INTO cllg_academico_pensum_detail (FK_PENSUM, FK_MATERIA) VALUES ('{$intID}', '{$arrPost1["value"]}')");
            }            
        }
    }
    else {
        $db->query("UPDATE cllg_academico_pensum SET {$strFields} WHERE ID = '{$strId}'");
        $db->query("DELETE FROM cllg_academico_pensum_detail WHERE FK_PENSUM = '{$strId}'");
        reset($_POST);
        while( $arrPost1 = each($_POST) ) {
            $arrExplode = explode( "_", $arrPost1["key"]);
            if( $arrExplode[0] == "hdnAcademicoPensumD" ) {
                $db->query("INSERT INTO cllg_academico_pensum_detail (FK_PENSUM, FK_MATERIA) VALUES ('{$strId}', '{$arrPost1["value"]}')");
            }            
        }
    }
    
    
}
?>